import request from './request'

// 登录接口
export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  user: {
    id: number
    username: string
    email: string
    nickname?: string
    avatar?: string
    roles: string[]
  }
}

// 注册接口
export interface RegisterRequest {
  username: string
  email: string
  password: string
  nickname?: string
}

export interface RegisterResponse {
  message: string
  user: {
    id: number
    username: string
    email: string
    nickname?: string
  }
}

// 登录
export const login = (data: LoginRequest): Promise<LoginResponse> => {
  return request.post('/api/auth/login', data)
}

// 注册
export const register = (data: RegisterRequest): Promise<RegisterResponse> => {
  return request.post('/api/auth/register', data)
}

// 登出
export const logout = (): Promise<void> => {
  return request.post('/api/auth/logout')
}

// 获取当前用户信息
export const getCurrentUser = (): Promise<LoginResponse['user']> => {
  return request.get('/api/user/profile')
}

// 更新用户信息
export const updateUserProfile = (data: Partial<LoginResponse['user']>): Promise<LoginResponse['user']> => {
  return request.put('/api/user/profile', data)
}

// 刷新token
export const refreshToken = (): Promise<{ token: string }> => {
  return request.post('/api/auth/refresh')
}
